var regg;
function checkPhone() {
    var phone = document.getElementById("userphone");
    regg = /^[1][3,4,5,7,8][0-9]{9}$/;

    if(!regg.test(phone.value)) {
        phone.style.borderColor = "red";
        return false;
    }else {
        phone.style.borderColor = "darkgray";
        return true;
    }
}

var pwd;
function checkPwd() {
    var userpassword = document.getElementById("pwd");
    pwd = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/;

    if(!pwd.test(userpassword.value)) {
        userpassword.style.borderColor = "red";
        return false;
    }else {
        userpassword.style.borderColor = "darkgray";
        return true;
    }
}
$("#lo").on("click",function(){
    location.href="/login1";    //跳转登录页面
    return false;
})
var Uname;
function checkUname() {
    var uname = document.getElementById("uname");
    Uname = /^[\u4E00-\u9FA5]{2,10}(·[\u4E00-\u9FA5]{2,10}){0,2}$/;

    if(!Uname.test(uname.value)) {
        uname.style.borderColor = "red";
        return false;
    }else {
        uname.style.borderColor = "darkgray";
        return true;
    }
}



// 进行注册操作
layui.use(['table', 'laydate','form'], function() {
    var form = layui.form;
    form.on('submit(regis)', function (data) {

        data = data.field;
        console.log(data);
        $('.eachIn input').each(function(i){
            console.log($(this).val() === '');
            if($(this).val() === ''){
                layer.msg('请完善注册信息');
                return false;
            }
        });
        // if(data.username === '') {
        //     layer.msg('姓名不能为空');
        //     return false;
        // }
        // if (data.userpassword === '') {
        //     layer.msg('密码不能为空');
        //     return false;
        // }
        // if (data.userrealname === '') {
        //     layer.msg('请输入真实姓名');
        //     return false;
        // }
        // if (data.useramail === '') {
        //     layer.msg('请输入电子邮箱');
        //     return false;
        // }
        // if (data.userPhone === '') {
        //     layer.msg('电话不能为空');
        //     return false;
        // }
        if(!pwd.test(data.userPwd)){
            layer.msg('密码格式错误');
            return false;
        }
        if(!Uname.test(data.userName)){
            layer.msg('姓名格式错误');
            return false;
        }
        if(!regg.test(data.userPhone)){
            layer.msg('手机号格式错误');
            return false;
        }
        $.ajax({
            type:'post',
            url:'/redister',
            contentType: 'application/json; charset=UTF-8',
            async:false,
            dataType:'json',
            data:JSON.stringify(data),
            success:function(res){
                console.log("res为:"+res)
                if(res.state == '200'){
                    layer.msg(res.msg,{time:1800,icon:6},function (){
                        console.log("res为:"+res.data);
                        window.location = '../../login.html';
                    });
                }else{
                    layer.msg(res.msg,{time:1000,icon:5});
                }
            }
        })

    });
})
